qemu_option=""
qemu_net_id=""

help_info=$(cat <<-END
Usage: qemu-run [OPTION]...
Run a OHOS image in qemu according to the options.

    Options:
    -e,  --exec image_path    build images path, including: zImage-dtb, ramdisk.img, system.img, vendor.img, userdata.img
    -g,  --gdb                enable gdb for kernel
    -n,  --net id             auto configure network, support opening multiple devices
    -h,  --help               print help info
END
)

function echo_help()
{
    echo "${help_info}"
    exit 0
}

function qemu_option_add()
{
    qemu_option+=" "
    qemu_option+="$1"
}

function parameter_verification()
{
    if [ $1 -eq 0 ] || [ x"$(echo $2 | cut -c 1)" = x"-" ]; then
        echo_help
    fi
}

function qemu_network()
{
    ifconfig br0 > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        qemu_install_path='/usr/local'
        sudo modprobe tun tap
        sudo ip link add br0 type bridge
		sudo ip address add 10.0.2.2/24 dev br0
		sudo ip link set dev br0 up
        [ ! -d ${qemu_install_path}/etc/qemu ] && sudo mkdir -p ${qemu_install_path}/etc/qemu
        echo 'allow br0' | sudo tee -a ${qemu_install_path}/etc/qemu/bridge.conf
        echo 0 | sudo tee /proc/sys/net/bridge/bridge-nf-call-iptables
        sudo brctl addbr br0
        sudo ifconfig br0 up
        sudo ifconfig br0 10.0.2.2/24
    fi

    qemu_option_add "-netdev bridge,id=net0 -device virtio-net-device,netdev=net0,mac=12:22:33:44:55:66"
}

function copy_img()
{
    cp $elf_file/userdata.img $elf_file/userdata${qemu_net_id}.img
    cp $elf_file/vendor.img $elf_file/vendor${qemu_net_id}.img
    cp $elf_file/system.img $elf_file/system${qemu_net_id}.img
    cp $elf_file/updater.img $elf_file/updater${qemu_net_id}.img
}

elf_file="out/qemu-arm-linux/packages/phone/images"

while [ $# -ne 0 ]
do
    case $1 in
        "-e")
            shift
            parameter_verification $# $1
            elf_file="$1"
            ;;
        "-n")
            qemu_network
            ;;
        "-g")
            qemu_option_add "-s -S"
            ;;
        "-h")
            echo_help
            ;;
    esac

    shift
done

[ x"${qemu_net_id}" != x ] && copy_img

qemu_option_add "-drive if=none,file=$elf_file/userdata${qemu_net_id}.img,format=raw,id=userdata,index=3 -device virtio-blk-device,drive=userdata"
qemu_option_add "-drive if=none,file=$elf_file/vendor${qemu_net_id}.img,format=raw,id=vendor,index=2 -device virtio-blk-device,drive=vendor"
qemu_option_add "-drive if=none,file=$elf_file/system${qemu_net_id}.img,format=raw,id=system,index=1 -device virtio-blk-device,drive=system"
qemu_option_add "-drive if=none,file=$elf_file/updater${qemu_net_id}.img,format=raw,id=updater,index=0 -device virtio-blk-device,drive=updater"
qemu_option_add "-kernel $elf_file/zImage-dtb -initrd $elf_file/../../../ramdisk.img"

sudo qemu-system-arm -M virt -cpu cortex-a7 -smp 4 -m 1024 -nographic \
    ${qemu_option} \
    -append "console=ttyAMA0,115200 init=/bin/init hardware=qemu.arm.linux default_boot_device=a003e00.virtio_mmio root=/dev/ram0 rw"
